<!doctype html>
<script src="/js-test-resources/js-test.js"></script>
<script type="text/javascript">
window.jsTestIsAsync = true;
description('XMLHttpRequest.responseText should not throw even when not (loading or done).');
function test(readyState) {
    var xhr;
    return new Promise(function(resolve, reject) {
        xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function() {
            try {
                if (this.readyState === readyState) {
                    // We evaluate responseText because we want to make sure
                    // doing that doesn't throw.
                    var response = xhr.responseText;
                    resolve(response);
                }
            } catch(e) {
                reject(e);
            }
        }
        xhr.onerror = reject;
        xhr.open('GET', 'resources/1251.html');
        if (readyState !== 1) {
            xhr.send();
        }
    }).then(function() {
        testPassed('readyState = ' + readyState);
        xhr.abort();
    }, function(e) {
        testFailed('readyState = ' + readyState, + ', ' + e);
    });
}

var promise = Promise.resolve();
for (var readyState = 1; readyState <= 4; ++readyState) {
    promise = promise.then(test.bind(undefined, readyState));
}
promise.then(finishJSTest, function(e) {
    testFailed(e);
    finishJSTest();
});
</script>
